//导入express
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');

const app = express();

const route_water = require('./routes/water');
const route_origin = require('./routes/originData');

// 使用body-parser中间件解析请求体
app.use(bodyParser.urlencoded({ extended: true, charset: 'utf-8' }));
app.use(bodyParser.json({ charset: 'utf-8' }));
app.use(cors());//允许跨域请求



app.use("/water", route_water)
app.use("/origin", route_origin)

app.get('/', (req, res) => {
    // 数据库操作
    // 响应并返回数据
    res.send('你好，世界');
});

// 启动服务器
app.listen(3000, () => {
    console.log('Server started on port 3000 http://localhost:3000/');
});

// 404 错误处理中间件
app.use((req, res, next) => {
    res.status(404).sendFile(path.join(__dirname, "pet2", "404.html"));
});

// 服务器错误处理中间件
app.use((err, req, res, next) => {
    console.error(err.stack);
    res.status(500).send("Internal Server Error");
});

